package jermedit.gobjekte;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;
import javax.swing.JOptionPane;


public abstract class PolygonObject implements Serializable, Saveable{
    protected int id;
    protected int breite; // width  ( delta X)-

    
    protected int laenge; // height ( delta Y)|
    protected int x,y;
    private boolean selected = false;         
    


    protected String name = "";
    
    public EditPoint obenmitte, mittelinks, mitterechts, untenmitte;
         //obenlinks, obenrechts,untenlinks, untenrechts;


    protected Rectangle boundingBox;

    public Rectangle getBoundingBox() {
        return boundingBox;
    }
 
    public abstract boolean isInArea(int x, int y);
    public abstract void drawSub(Graphics2D g2);
    
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public void setPos (int x, int y){
		this.x=x;
		this.y=y;
    }
    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
    
    public void draw(Graphics2D g2) {
            // falls ausgewählt
            if (this.selected){
                g2.setPaint(Color.blue);               
            }

            this.drawSub(g2);
        
            // falls ausgewählt, sollen alle vorher gesetzen Variablen neu gesetzt werden
            if (this.selected){
                g2.setPaint(Color.black);
            } 
           
            
    }
    
    public void move(int x, int y){
        this.x = x;
        this.y = y;
    }
    
    public void umbenennen(String name) {
        this.name = name;
    }
    
    public void umbenennen() {
        String name="";
        name = JOptionPane.showInputDialog("Name:", this.name);
        if  (name==(null)){
            JOptionPane.showMessageDialog(null,"Vorgang abgebrochen!");
            }//abfangen falls kein Name eingegeben wird
        else  
        if (name.length()==0){
            JOptionPane.showMessageDialog(null,"Keinen Wert eingegben!");   
            }
        else{
            this.name = name;
        }
    }
    
    // Used in the Creation of Objects
    public boolean bUmbenennen() {
        String name;
        name = JOptionPane.showInputDialog("Name:", this.name);
        if  (name == (null) ){
            JOptionPane.showMessageDialog(null,"Vorgang abgebrochen!");
            }//abfangen falls kein Name eingegeben wird
        else  
        if (name.length()==0){
            JOptionPane.showMessageDialog(null,"Keinen Wert eingegben!");   
            }
        else{
            this.name = name;
        }
        
        return ( !(name == (null)) );
    }
    
    public Point linkerObenEckpunkt(){
        Point liOben = new Point(x-getBoundingBox().width/2,y-getBoundingBox().height/2);
        return liOben;
    }
        
    public Point rechterObenEckpunkt(){
        Point reOben = new Point(x+getBoundingBox().width/2,y-getBoundingBox().height/2);
        return reOben;
    }
    
    public Point linkerUntenEckpunkt(){
        Point liUnten = new Point(x-getBoundingBox().width/2,y+getBoundingBox().height/2);
        return liUnten;
    }
    
    public Point rechterUntenEckpunkt(){
        Point reUnten = new Point(x+getBoundingBox().width/2,y+getBoundingBox().height/2);
        return reUnten;
    }
    
    public Point mitteLinks(){
        Point liMi = new Point(x-getBoundingBox().width/2,y);
        return liMi;
    }
        
    public Point mitteOben(){
        Point miOben = new Point(x,y-getBoundingBox().height/2);
        return miOben;
    }
    
    public Point mitteRechts(){
        Point miRechts = new Point(x+getBoundingBox().width/2,y);
        return miRechts;
    }
    
    public Point mitteUnten(){
        Point miUnten = new Point(x,y+getBoundingBox().height/2);
        return miUnten;
    }
    
    public int getBreite() {
        return breite;
    }

    public void setBreite(int breite) {
        this.breite = breite;
    }

    public int getLaenge() {
        return laenge;
    }

    public void setLaenge(int laenge) {
        this.laenge = laenge;
    }

    
    public int getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }

    public EditPoint getObenmitte() {
        return obenmitte;
    }

    public EditPoint getMittelinks() {
        return mittelinks;
    }

    public EditPoint getMitterechts() {
        return mitterechts;
    }

    public EditPoint getUntenmitte() {
        return untenmitte;
    }
    
    // (int x, int y, int id)  ZO , x, y, id, breite, laengem name  - create zuSub object with x,y,id, add breite, länge, name ; add to list
    public String getObjectInString(){
//        return "zo:"+ this.getClass().getSimpleName() + ";"   + "x:" + x + ";" + "y:" + y+ ";" + "id:" + id + ";" + "breite:" + breite + ";" + "laenge:" + laenge + ";" + "name:" + name + ";";
        return "zo:"+ this.getClass().getSimpleName() + ";"   + x + ";" +  y+ ";" +  id + ";" +  breite + ";" +  laenge + ";" +  name + ";";
    }
}


